import request from './request.js'
import { API_URL } from '../env.js'

const install = (Vue, vm) => {
  // #ifdef H5
  request.setConfig({
    baseUrl: API_URL,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
      'token': uni.getStorageSync('token')
    },
  })
  // #endif

  // #ifdef APP-PLUS
  uni.showLoading({
    title: '加载中',
  })
  uni.request({
    url: 'http://move.lhxns.com/api/index/getlink',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'token': uni.getStorageSync('token')
    },
    method: 'POST',
    responseType: 'text',
    success: (res) => {
      if (res.data.code == 1) {
        uni.setStorageSync('baseUrl', res.data.data)
        uni.hideLoading()

        request.setConfig({
          baseUrl: uni.getStorageSync('baseUrl'),
          header: {
            'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
          },
        })
      } else {
        vm.$common.errorToShow('getlink-请求失败')
        uni.hideLoading()
      }
    },
    fail: (err) => {
      vm.$common.errorToShow(err)
      uni.hideLoading()
    },
  })

  // #endif

  //请求拦截，配置Token等参数
  request.interceptor.request(function (type, config) {
    config.header.token = uni.getStorageSync('token')
      ? uni.getStorageSync('token')
      : ''

    return config
  })

  //响应拦截，判断状态码是否通过
  request.interceptor.response(
    function (type, res) {
      // console.log(type, res, '---------success')
      if (type == 'request') {
        if (res.code == 0) vm.$common.errorToShow(res.reason)

        return res
      }
    },
    function (type, err) {
		console.log(err)
      if (!err) {
        vm.$common.errorToShow('操作失败~')
        return
      }
      if (err.code == 401) {
        vm.$common.errorToShow(err.reason)
      } else vm.$common.errorToShow('操作失败~')
      return err
    }
  )
  // 登录
    vm.$api.login = (data = {}) => request.post('index.php/api/user/login', data)
  vm.$api.userInfo = (data = {}) => request.post('index.php/api/user/selUserInfo', data)
  vm.$api.getuserInfo = (data = {}) => request.post('index.php/api/user/user_mobile', data)
    vm.$api.shouluanxinxi = (data = {}) => request.post('index.php/api/user/getUserData', data)
  vm.$api.ismaster = (data = {}) => request.post('index.php/api/user/usertree', data)
  // 验证支付方式及地址
  vm.$api.isValidAddressAndPay = (data = {}) => request.post('index.php/api/user/selAddressPay', data)

  //用户注册
  vm.$api.register = (data = {}) => request.post('index.php/api/user/register', data)
  // 获取验证码
  vm.$api.smsSend = (data = {}) => request.post('index.php/api/sms/send', data)
  // 获取图片验证码
  vm.$api.getCode = (data = {}) => request.post('index.php/api/user/checkcode', data)
  // 首页轮播
  vm.$api.bannerList = (data = {}) => request.post('index.php/api/user/index_banner?shop_id=1', data)
// 首页其他
  vm.$api.home = (type = 0) => request.post('index.php/api/user/indexinfo?type=' + type)
  vm.$api.jingpai_list = (type = 0) => request.post('index.php/api/user/sel_classified_treasure_pavilionbanner?type=' + type)

// 	可以竞拍的数据
  vm.$api.qiangdan_list = (id) => request.post('index.php/api/market_product/selResale?shop_id=' + id)
  // 预约
  vm.$api.yuyue = (id) => request.post('/index.php/api/shop/booking?shop_id=' + id)

  // 抢单
  vm.$api.qiangdan = (sn = '07crd4tgojk4kzsf1xoz9fpu2hseq7nt') => request.post('/index.php/api/market_order/add?product_sn=' + sn)
 //订单
  vm.$api.orderList = (data) => request.post('/index.php/api/market_order/orderlist', data)
  vm.$api.orderList2 = (data) => request.post('/index.php/api/market_order/selSplitOrderList', data)
  vm.$api.orderList3 = (data) => request.post('/index.php/api/market_product/goodsgotoList', data)
  vm.$api.chaifen_shenheList = (data) => request.post('/index.php/api/market_order/splitList', data)
  vm.$api.reslove_chaifen = (data) => request.post('/index.php/api/market_product/goodsplit', data)
  vm.$api.reject_chaifen = (data) => request.post('/index.php/api/market_product/refuseGoodSplit', data)
  // 封存列表
  vm.$api.fengcunList = (data) => request.post('/index.php/api/market_product/goodsList', data)
  vm.$api.weimaiList = (data) => request.post('/index.php/api/market_product/myMarketClound', data)
  vm.$api.fengcun = (data) => request.post('/index.php/api/market_product/freezegood', data)
  vm.$api.jiefeng = (data) => request.post('/index.php/api/market_product/openfgood', data)
  // 专场
  vm.$api.zhuanchang = (data) => request.post('/index.php/api/market_product/allgoodsgotoapply', data)
  vm.$api.zhuanchangpass = (data) => request.post('/index.php/api/market_product/allgoodsgotoapply', data)
  vm.$api.searchValue = (data) => request.post('/index.php/api/user/sel_user_money', data)

// 付款
  vm.$api.pay = (data) => request.post('/index.php/api/market_order/paymoney', data)
  // 收款
  vm.$api.pay2 = (data) => request.post('/index.php/api/market_order/confirmaccept', data)
  // 代卖
  vm.$api.sell = (data) => request.post('/index.php/api/market_order/allResellPass', data)
    // 对公账户
    vm.$api.getAccount = (data) => request.post('/index.php/api/market_product/bankOfDeposit', data)
// 拆分
    vm.$api.chaifen = (data) => request.post('/index.php/api/market_product/updateResale', data)
    vm.$api.tihuo = (data) => request.post('/index.php/api/user/addPickUpGoods', data)

  // 转单
  vm.$api.zhuandan = (data) => request.post('/index.php/api/user/assignToOneself', data)
  vm.$api.zhipaidingdan = (data) => request.post('/index.php/api/user/zhipaidingdan', data)

  vm.$api.user_baoge = (data) => request.post('/index.php/api/user/user_baoge', data)

  // 商品列表
  vm.$api.productList = (data) => request.post('/index.php/api/goods/getList', data)
  vm.$api.dhdddetails = (data = {}) =>
      request.post('/index.php/api/goods/orderdetail', data)
  vm.$api.userdefaultaddress = (data = {}) =>
      request.post('/index.php/api/useraddress/index_is', data)

  vm.$api.getuserdefaultaddress = (data = {}) =>
      request.post('/index.php/api/useraddress/index_sel', data)
  vm.$api.buy = (data) => request.post('/index.php/api/goods/buy', data)
// 提现审核列表
  vm.$api.tixianauditlist = (data = {}) =>
      request.post('/index.php/api/user/tixianauditlist', data)
  // 提现审核通过
  vm.$api.tixianpass = (data = {}) => request.post('/index.php/api/user/tixianpass', data)
  // 提现审核通过
  vm.$api.tixianject = (data = {}) => request.post('/index.php/api/user/tixianjujue', data)
  // 提现审核详情
  vm.$api.tixiandetail = (data = {}) =>
      request.post('/index.php/api/user/tixiandetail', data)

  // 兑换订单列表
  vm.$api.dhddlist = (data = {}) =>
      request.post('/index.php/api/goods/orderlist', data)

  // 兑换订单列表
  vm.$api.dhdddetails = (data = {}) =>
      request.post('/index.php/api/goods/orderdetail', data)


  // 兑换确认收货
  vm.$api.qrsh = (data = {}) =>
      request.post('/index.php/api/goods/orderReturn', data)

  // 个人中心
  vm.$api.updateUser = (data) => request.post('index.php/api/user/editData', data)
  vm.$api.dhc_list = (type, page, pagesize) => request.post('index.php/api/user/jflog', { type, page, pagesize })
  vm.$api.yongjin_list = (type, page, pagesize) => request.post('index.php/api/user/tuiguangmingxi', { page, pagesize })
  vm.$api.zhuanzeng = (data) => request.post('index.php/api/user/selReciprocalRotation', data)
  vm.$api.tixian = (data) => request.post('index.php/api/user/tixian', data)
  vm.$api.hexiao2 = (data) => request.post('index.php/api/user/hexiao', data)
    vm.$api.baogezhuyuyuelist = (data) => request.post('index.php/api/user/baogezhuyuyuelist', data)
    vm.$api.updatebaogezhuyuyue = (data) => request.post('index.php/api/user/baogezhuyuyue', data)
 // 客户
  vm.$api.kehu = (data) => request.post('index.php/api/user/mytreeusers', data)


  // 地址
  vm.$api.address_list = (data) => request.post('index.php/api/useraddress/index', data)
  vm.$api.addaddress = (data) => request.post('index.php/api/useraddress/addaddress', data)
  vm.$api.editaddress = (data) => request.post('index.php/api/useraddress/editaddress', data)
  vm.$api.addressdetail = (data = {}) =>
      request.post('/index.php/api/useraddress/detail', data)
    // 其他

    vm.$api.getOrderPayPhotoList = (data = {}) => request.post('/index.php/api/user/paypics', data)
}


export default {
  install,
}
